package com.sevenGroup.pawnshop.controller;


import com.sevenGroup.pawnshop.Utils.ParameterCalibrationUtil;
import com.sevenGroup.pawnshop.Utils.Result;
import com.sevenGroup.pawnshop.controller.form.AccountAndUserForm;
import com.sevenGroup.pawnshop.controller.form.BrandForm;
import com.sevenGroup.pawnshop.exception.sevenGroupException;
import com.sevenGroup.pawnshop.model.Account;
import com.sevenGroup.pawnshop.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.http.util.Args;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */

@RestController
@RequestMapping("/account")
@Api(tags="账号相关：登陆，查询等")
@Validated
public class AccountController {
    @Resource
    private AccountService as;

    @ApiOperation("此方法可以新增账号")
//  新增账号
    @PostMapping("addAccount")
    @PreAuthorize("hasAnyAuthority('boss')")
    public Result addAccount(@ApiParam("需要传入账号对象") @RequestBody @Valid AccountAndUserForm accountForm,BindingResult bindingResult) throws sevenGroupException {
        ParameterCalibrationUtil.ParameterCalibration(bindingResult);
        Integer num=as.addAccount(accountForm);
        if (num==1) return Result.success(num);
        return Result.fail(num);
    }

    @ApiOperation("此方法可以查询账号是否存在，可以用于注册校验是否存在相同的用户名")
//  查询是否有某个账号(通过账号)
    @PostMapping("getAccountByAccountUsername")
    @PreAuthorize("hasAnyAuthority('boss','shopManage','worker','noWorker')")
    public Result getAccountByAccountUsername(@ApiParam("传入需要查询的用户名") String accountUsernam){
        Boolean aBoolean=as.getAccountByAccountUsername(accountUsernam);
        if (aBoolean) return Result.fail("已被注册");
        return Result.success("该账号可用");
    }

    @ApiOperation("此方法可以查询手机号是否存在，可以用于注册校验是否存在相同的手机号")
//  查询是否有某个手机号(通过手机号)
    @PostMapping("getAccountByAccountTel")
    @PreAuthorize("hasAnyAuthority('boss','shopManage','worker','noWorker')")
    public Result getAccountByAccountTel(@ApiParam("传入需要查询的手机号名") String accountTel){
        Boolean aBoolean=as.getAccountByAccountTel(accountTel);
        if (aBoolean) return Result.fail("已被注册");
        return Result.success("该手机号可用");
    }

    @ApiOperation("此方法通过账号ID可以修改账号信息，例如密码，账号，手机号。在更新时会自动判断是否有相同的手机号和账号存在")
//  根据账号ID修改账号
    @PostMapping("updateAccount")
    @PreAuthorize("hasAnyAuthority('boss','shopManage','worker','noWorker')")
    public Result updateAccount(@RequestBody @ApiParam("账号对象") Account account){
        String message=as.updateAccount(account);
        if ("修改成功".equals(message)) return Result.success(message);
        return Result.fail(message);
    }
}

